package net.matt2000.android.search.wp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ShortcutChooser extends ListActivity{
	
	private static final String LANGUAGE = "language";
	private static final String TEXT = "text";
	private static final String ICON = "icon";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        List<Map<String, Object>> siteList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < getResources().getStringArray(R.array.language).length; i++) {
        	Map<String, Object> site = new HashMap<String, Object>();
        	site.put(LANGUAGE, getResources().getStringArray(R.array.language)[i]);
        	site.put(TEXT, getResources().getStringArray(R.array.website)[i]);
        	site.put(ICON, getResources().obtainTypedArray(R.array.icon).getResourceId(i, R.drawable.icon));
        	siteList.add(site);
        }

        setListAdapter(new SimpleAdapter(this, siteList, R.layout.row,
        		new String[] { TEXT, ICON },
        		new int[] { R.id.text, R.id.icon } ));
        
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	Map<String, Object> item= (Map<String, Object>)getListAdapter().getItem(position);
    	String language = (String) item.get(LANGUAGE);
    	int languageNumber = SuggestionProvider.getLanguageNumber(this, language);
    	String name = getResources().getStringArray(R.array.app_name)[languageNumber];
    	int iconId = getResources().obtainTypedArray(R.array.icon).getResourceId(languageNumber, R.drawable.icon);
    	
    	Intent search = new Intent(this, SearchActivity.class);
    	search.putExtra(SearchActivity.LANGUAGE, language);
    	Intent shortcut = new Intent(Intent.ACTION_CREATE_SHORTCUT);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, search);
        Intent.ShortcutIconResource sir = Intent.ShortcutIconResource.fromContext(this, iconId);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, sir);
        
        setResult(RESULT_OK, shortcut);
        finish();
    } 

}
